package ztc.servlet;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import ztc.common.SiginBean;
import ztc.common.StuBean;
import ztc.service.QRCodeMaker;
import ztc.service.QRStatus;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Hashtable;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by ztc on 16-3-31.上午11:17
 */
public class QRCode extends javax.servlet.http.HttpServlet {
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        HttpSession session=request.getSession(false);
        ServletContext application=this.getServletContext();
        if(session!=null){
            SiginBean siginBean= (SiginBean) session.getAttribute("siginBean");
            //二维码尺寸
            int size=siginBean.getSize();
            //二维码前景色
            Color foreground;
            String pForeground= siginBean.getForeground();
            if(pForeground.equals("white")){
                foreground=Color.WHITE;
            }else if(pForeground.equals("light-gray")){
                foreground=Color.LIGHT_GRAY;
            }else{
                foreground=Color.ORANGE;
            }
            //二维码背景色
            Color background;
            String pBackground= siginBean.getBackground();
            if(pBackground.equals("red")){
                background=Color.RED;
            }else if(pBackground.equals("blue")){
                background=Color.BLUE;
            }else{
                background=Color.BLACK;
            }
            //生成唯一标识
            String uuid= UUID.randomUUID().toString();
            session.setAttribute("uuid",uuid);
            //清除hashmap里的失效的uuid
            QRStatus.rmNull(application,session);
            //将信息放到application
            if(application.getAttribute("sessionID")==null){
                ConcurrentHashMap<String,ConcurrentHashMap<String,StuBean>> aMap=
                        new ConcurrentHashMap<String, ConcurrentHashMap<String, StuBean>>();
                ConcurrentHashMap<String,StuBean> sMap=new ConcurrentHashMap<String, StuBean>();
                sMap.put(uuid,new StuBean("null","null","null","null","null","null"));
                aMap.put(session.getId(),sMap);
                application.setAttribute("sessionID",aMap);
            }else{
                ConcurrentHashMap<String,ConcurrentHashMap<String,StuBean>> aMap=
                        (ConcurrentHashMap<String, ConcurrentHashMap<String, StuBean>>) application.getAttribute("sessionID");
                if(aMap.containsKey(session.getId())) {
                    ConcurrentHashMap<String, StuBean> sMap =
                            aMap.get(session.getId());
                    sMap.put(uuid, new StuBean("null","null","null","null","null","null"));
                    System.out.println("有aMap,sMap");
                }else{
                    ConcurrentHashMap<String,StuBean> sMap=new ConcurrentHashMap<String, StuBean>();
                    sMap.put(uuid, new StuBean("null","null","null","null","null","null"));
                    aMap.put(session.getId(),sMap);
                    System.out.println("有aMap");
                }
            }
            //构造链接
            String myurl=request.getScheme()+"://"
                    +request.getServerName()+":"
                    +request.getServerPort()+"/users/qrsiginin?uuid="+uuid
                    +"&sessionID="+session.getId()
                    //+"&stuID="+"1231231"
                    //+"&name="+"name"
                    +"&sex="+ "男";
            //生成二维码
            try {
                Hashtable<EncodeHintType,String> hints =
                        new Hashtable<EncodeHintType,String>();
                // 内容所使用编码
                hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
                BitMatrix bitMatrix = new MultiFormatWriter().encode(
                        myurl, BarcodeFormat.QR_CODE,size,size,hints);
                new QRCodeMaker(foreground,background)
                        .writeToStream(bitMatrix, response.getOutputStream(), "gif",application,session);
            } catch (WriterException e) {
                e.printStackTrace();
            }
        }else{
            request.getRequestDispatcher("/qr/issuance.jsp").forward(request,response);
        }
    }
}
